
#include "MCU_include.h"
volatile	bit HF_Pwm_Freq_update_flag=0;
	vu8  HF_fjtmr2_counter,PWMPH_buffer,PWMPL_buffer;
	vu16 HF_Counter,sum_16_HF,fjtmer_state, PWM_HF_buffer;
void HF_interrupt_Init()
{
//	_ctm1ae=1;		//ctm1 comparator A match interrupt control,
//	_mf7e =1;		//multi-function interrupt 7 control,ctm1 P,A match,TMCA,LVD 
	

	_mf0e =1;		//multi-function interrupt 0 control,OVP6,OVP7,OVP8
//	_usime =1;		//USIM interrupt control,
	
	
	_mf2e =1;		//multi-function interrupt 2 control,FJTM,FJEQU
	_fjtme =1;		//FJTMR interrupt control
	
//	_mf3e =1;		//multi-function interrupt 3 control,CTM3A,CTM3P
//	_ctm3ae=1;		//ctm3 comparator A match interrupt control,
}



void HF_Init()
{
	_fjtmc = 0b00000001;  //Bit 1~0 FJTMPSC1 ~ FJTMPSC0 : 01 -> fh/256 -> 16M/256 -> 1/FTIMER = 16us fjtime計數器分頻
	
	_fjtmr1 = ( 256-FJTMR_TIME1 );				

	_fjtmr2 =(	256-FJTMR_TIME2 );	

	_fjtmr3 =( 256-FJTMR_TIME3 );					

	_fjtmr4 = ( 256-FJTMR_TIME4 );

	
	_fjmds=  0;		
	_fjsmds= 0;		
	_fjhmds= HF_Shake_ON_Range;		
	_fjtss=  HF_Shake_ON_Trigger_Method;		
	_fjotss= 0;	

	_fjc1 = 0b00000000;

	_fjm1c0=0b00110000;		//	_fjm1c0=0b00110000;

	_fjm1c1=0b01110000;

	_fjm1c2=0b00000000;

	_fjm1c3=0b00000000;	

	_fjm0c0=0b00110000; 
			
	
volatile unsigned long DT0_min_time = 0;	
    DT0_min_time = (unsigned long)(DEAD0_1_Min_TIME)*100;	
    DT0_min_time/=3125;	
	_dtminl	=(unsigned char)(DT0_min_time - 1) & 0x0ff;							
	_dtminh=(unsigned char)(DT0_min_time - 1) >> 8;			
				
#ifdef HF_MODE    
	_fjton=HF_Shake_FJTIME_OFF;		
	_fjen=HF_Shake_ON;		
	_fjwmd=  1;
	HF_Pwm_Freq_update_flag = 1 ;
	
	
#endif
}





void  HF_Pwm_Freq_update()
{


//	if(PWM_F > HF_Shake_Frequency_Coarse_Control_Grade1)
	if(u16_PowerSet <= 1600)
	{
		PWM_HF_buffer = PWM_SF/(PWM_F-1);				////Convert PWM frequency to the value that needs to be filled in the 12 bit register

		_fjpal	=  (PWM_HF_buffer-HF_Shake_FJPA_35K) & 0x0ff;			//Second time zone PWM approximation mode target value JFPA 
		_fjpah	= (PWM_HF_buffer-HF_Shake_FJPA_35K) >>8;
		
		_fjpbl	=  (PWM_HF_buffer+HF_Shake_FJPB_35K) & 0x0ff;			//Third time zone PWM approximation mode target value JFPB
		_fjpbh	= (PWM_HF_buffer+HF_Shake_FJPB_35K) >>8;		
	}

	if(u16_PowerSet <= 2300)
	{
		PWM_HF_buffer = PWM_SF/(PWM_F-1);			
					
		_fjpal	=  (PWM_HF_buffer-HF_Shake_FJPA_25K) & 0x0ff;			 
		_fjpah	= (PWM_HF_buffer-HF_Shake_FJPA_25K) >>8;
		
		_fjpbl	=  (PWM_HF_buffer+HF_Shake_FJPB_25K) & 0x0ff;		
		_fjpbh	= (PWM_HF_buffer+HF_Shake_FJPB_25K) >>8;		
	}
	else 
	{
		PWM_HF_buffer = PWM_SF/(PWM_F-1);			
					
		_fjpal	=  (PWM_HF_buffer-HF_Shake_FJPA_20K) & 0x0ff;			 
		_fjpah	= (PWM_HF_buffer-HF_Shake_FJPA_20K) >>8;
		
		_fjpbl	=  (PWM_HF_buffer+HF_Shake_FJPB_20K) & 0x0ff;		
		_fjpbh	= (PWM_HF_buffer+HF_Shake_FJPB_20K) >>8;		
	}
	
		if (PWM_HF_buffer>HF_PWM_MAX_FRE) 						 // pwm  limite
		{	
			PWM_HF_buffer=HF_PWM_MAX_FRE; 			
		}
		else if (PWM_HF_buffer<HF_PWM_MIN_FRE)	
		{
			PWM_HF_buffer=HF_PWM_MIN_FRE;
		}	
}


/******************* End of  file ***************/